package com.hankol.feature_wine_meal

import androidx.lifecycle.Observer
import com.alibaba.android.arouter.facade.annotation.Route
import com.baselibrary.Constants
import com.baselibrary.activity.BaseActivity
import com.baselibrary.extension.checkNotNull
import com.baselibrary.router.ActivityRouter
import com.baselibrary.utils.ARouterUtils
import com.hankol.feature_wine_meal.adapter.FineFoodAdapter
import com.hankol.feature_wine_meal.bean.FoodDetailsBean
import com.hankol.feature_wine_meal.databinding.ActivityFineFoodBinding
import com.hankol.feature_wine_meal.viewmodel.WineFoodViewModel
import com.scwang.smartrefresh.layout.api.RefreshLayout
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener

/**
 * @description 时令美食
 * @author 关忆北、
 * @time 2022/6/21 4:53 下午
 */
@Route(path = ActivityRouter.WINE_MEAL.WINE_MEAL_FINE_FOOD) class FineFoodActivity : BaseActivity() {
    private var mRecommendInfoId: Long = 0
    private var mRecommendInfoTitle: String = ""
    private var mPagerNum: Int = 1
    private lateinit var mViewModel: WineFoodViewModel
    private lateinit var mDataBinding: ActivityFineFoodBinding
    private val mAdapter: FineFoodAdapter by lazy { FineFoodAdapter() }

    companion object {
        const val WINE_MEAL_RECOMMEND_INFO_ID: String = "WINE_MEAL_RECOMMEND_INFO"
        const val WINE_MEAL_RECOMMEND_INFO_TITLE: String = "WINE_MEAL_RECOMMEND_INFO_TITLE"
    }

    override fun getLayoutId(): Int {
        initImmersion(true)
        return R.layout.activity_fine_food
    }

    override fun init() {
        mDataBinding = getDataBinding<ActivityFineFoodBinding>()
        mViewModel = getActivityViewModelProvider(this).get(WineFoodViewModel::class.java)
        intent.extras?.let {
            if (ARouterUtils.parseParams(it, WINE_MEAL_RECOMMEND_INFO_ID)[0].checkNotNull()) {
                mRecommendInfoId = ARouterUtils.parseParams(it, WINE_MEAL_RECOMMEND_INFO_ID)[0].toLong()
            }
            if (ARouterUtils.parseParams(it, WINE_MEAL_RECOMMEND_INFO_TITLE)[0].checkNotNull()) {
                mRecommendInfoTitle = ARouterUtils.parseParams(it, WINE_MEAL_RECOMMEND_INFO_TITLE)[0]
            }
        }

        loadData()
        initListener()
        initObserver()


    }


    fun loadData() {
        mDataBinding.tvFineFoodTitle.text = mRecommendInfoTitle
        mDataBinding.rvFineFoodList.adapter = mAdapter
        mViewModel.reqFoodRecommendInfo(mRecommendInfoId, mPagerNum)
    }


    fun initListener() {
        mDataBinding.smartFineFood.setEnableLoadMore(false)
        mDataBinding.smartFineFood.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
            override fun onRefresh(refreshLayout: RefreshLayout) {
                mPagerNum = 1
                mViewModel.reqFoodRecommendInfo(mRecommendInfoId, mPagerNum)

            }

            override fun onLoadMore(refreshLayout: RefreshLayout) {
                mViewModel.reqFoodRecommendInfo(mRecommendInfoId, mPagerNum)

            }
        })
        mAdapter.setOnItemClickListener { adapter, view, position ->
            val bean = adapter.data[position]
            if (bean is FoodDetailsBean) {
                val hashMap: HashMap<String, Any> = hashMapOf()
                hashMap[Constants.WINE_MEAL_DETAILS_ID] = bean.id
                hashMap["CLASS_NAME"] = ""
                hashMap["RECOMMEND_NAME"] = "时令美食"
                hashMap["DRINK_FOOD_PLATE_NAME"] = "精选专区"
                ARouterUtils.jump(ActivityRouter.WINE_MEAL.WINE_MEAL_DETAILS, hashMap)
            }
        }
        mDataBinding.ivFineFoodBack.setOnClickListener { finish() }
    }

    fun initObserver() {
        mViewModel.recommendInfoLD.observe(this, Observer { it ->
            mDataBinding.smartFineFood.finishRefresh()
            mDataBinding.smartFineFood.finishLoadMore()
            it.data?.let {
                if (it.isNotEmpty()) {
                    if (mPagerNum <= 1) {
                        mAdapter.setNewData(it)
                    } else {
                        mAdapter.addData(it)
                    }
                    mPagerNum++
                }

            }
        })
    }
}